'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const Favorite = app.model.define('favorite', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '收藏ID'
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID'
    },
    job_id: {
      type: INTEGER,
      allowNull: false,
      comment: '岗位ID'
    },
    job_info: {
      type: STRING(1000),
      allowNull: false,
      comment: '岗位信息（JSON格式存储）'
    },
    created_at: {
      type: DATE,
      allowNull: true,
      comment: '创建时间'
    },
    updated_at: {
      type: DATE,
      allowNull: true,
      comment: '更新时间'
    }
  }, {
    tableName: 'favorites',
    underscored: true
  });

  // 定义关联关系
  Favorite.associate = function() {
    // 一个收藏属于一个岗位
    app.model.Favorite.belongsTo(app.model.Job, {
      foreignKey: 'job_id',
<<<<<<< HEAD
=======
      targetKey: 'jobId',  // 明确指定关联的是 Job 的 jobId 字段
>>>>>>> resume
      as: 'favoriteJob'  // 修改别名，避免冲突
    });
  };

  return Favorite;
};